package cn.wolfcode.trip.admin.Controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductContent;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IProductContentService;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.service.IProductSortService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/** tiger
 * 商品控制器
 */
@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Autowired
    private IProductSortService sortService;

    @Autowired
    private IProductContentService contentService;

    /**
     * 查出所有的商品
     * 分页
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String query(@ModelAttribute("qo") QueryObject qo, Model model){
        PageInfo<Product> pageInfo= productService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("sorts",sortService.ListAll());
        return "/product/list";
    }

    /**
     * 保存修改商品
     * @param product
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Product product){
        JSONResult jsonResult =new JSONResult();
        try {
            productService.saveOrUpdate(product);
        } catch (Exception e) {
            jsonResult.mark("操作失败,请稍后再试");
            e.printStackTrace();
        }
        return jsonResult;
    }

    //删除商品
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        productService.delete(id);
        return new JSONResult();
    }

    /**
     * 根据商品id获取商品详情
     * @param id
     * @return
     */
    @RequestMapping("/getProductContentById")
    @ResponseBody
    public ProductContent getProductContentById(Long id){

        return contentService.getProductContentById(id);
    }
}
